﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using BTSDecompress.ClassLibrary;
using log4net;
namespace BTSDecompress.Controls
{
    public partial class QueryControl : UserControl
    {
        #region Variables
        string _currentFile = "";
        string _currentFileNameOnly = "";
        Boolean _isDirty = false;
        ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        #endregion
        #region Constructors
        public QueryControl()
        {
            InitializeComponent();
            sqlQuery = new SqlQuery();
        }
        #endregion
        #region Properties
        public string CurrentFile
        {
            get { return _currentFile; }
            set { _currentFile = value; }
        }
        public string CurrentFileNameOnly
        {
            get { return _currentFileNameOnly; }
            set { _currentFileNameOnly = value; }
        }
        public Boolean IsDirty
        {
            get { return _isDirty; }
            set { _isDirty = value; }
        }
        public DataTable OutputGridViewDataSource
        {
            get 
            {
                return (DataTable)outputGridView.DataSource;
            }
            set 
            {
                outputGridView.DataSource = value;
                nrOfRecordsLabel.Text = string.Format("{0} record(s)", outputGridView.RowCount.ToString());
            }
        }
        public TextBox OutputTextBox
        {
            get
            {
                return outputTextBox; 
            }
            set
            {
                outputTextBox = value;
            }
        }
        /// <summary>
        /// Get/set the value of the Query TextBox
        /// </summary>
        public FastColoredTextBoxNS.FastColoredTextBox Query
        {
            get
            {
                return queryTextBox;
            }
            set
            {
                queryTextBox = value;
            }
        }
        public SqlQuery sqlQuery;
        #endregion
        #region Methods
        /// <summary>
        /// Initializes some local variables and clear the Result TextBox
        /// </summary>
        public void ClearOutputControls()
        {
            log.Debug(string.Format("BEGIN - {0}", System.Reflection.MethodInfo.GetCurrentMethod().ToString()));

            // Clear the result textbox
            outputTextBox.Clear();
            outputTextBox.Refresh();
            nrOfRecordsLabel.Text="";

            // Clear the output grid
            outputGridView.DataSource = null;

            log.Debug(string.Format("END - {0}", System.Reflection.MethodInfo.GetCurrentMethod().ToString()));
        }
        /// <summary>
        /// Initializes the controls to certain default values
        /// </summary>
        public void ClearScreen()
        {
            log.Debug(string.Format("BEGIN - {0}", System.Reflection.MethodInfo.GetCurrentMethod().ToString()));

            SetDirty(false);
            _currentFile = "";
            _currentFileNameOnly = "";
            outputTextBox.Clear();
            outputGridView.DataSource = null;
            queryTextBox.Text = "";
            queryTextBox.Focus();

            log.Debug(string.Format("END - {0}", System.Reflection.MethodInfo.GetCurrentMethod().ToString()));
        }
        public void Redo()
        {
            queryTextBox.Redo();
        }
        /// <summary>
        /// In case a value has changed, the dirty bit is set and is reflected in the title of the screen
        /// </summary>
        /// <param name="dirty"></param>
        public void SetDirty(Boolean dirty)
        {
            log.Debug(string.Format("BEGIN - {0}", System.Reflection.MethodInfo.GetCurrentMethod().ToString()));

            _isDirty = dirty;

            if (_currentFileNameOnly != "")
            {
                Parent.Text = dirty ? string.Format("{0} (*)", _currentFileNameOnly) : string.Format("{0}", _currentFileNameOnly);
            }
            else
            {
                Parent.Text = "New Query.sql";
            }

            log.Debug(string.Format("END - {0}", System.Reflection.MethodInfo.GetCurrentMethod().ToString()));
        }
        public void Undo()
        {
            queryTextBox.Undo();
        }
        #endregion
        #region Events
        private void byteArrayContextMenuItem_Click(object sender, EventArgs e)
        {
            string tmp = "@ByteArray(" + queryTextBox.SelectedText + ")";
            Clipboard.SetText(tmp);
            queryTextBox.Paste();
        }
        private void cutContextMenuItem_Click(object sender, EventArgs e)
        {
            queryTextBox.Cut();
        }
        private void contextToTextMenuItem_Click(object sender, EventArgs e)
        {
            string tmp = "@ContextToText(" + queryTextBox.SelectedText + ")";
            Clipboard.SetText(tmp);
            queryTextBox.Paste();
        }
        private void copyContextMenuItem_Click(object sender, EventArgs e)
        {
            queryTextBox.Copy();
        }
        private void copyOutputGridContextMenuItem_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(outputGridView.GetClipboardContent());
        }
        private void deleteContextMenuItem_Click(object sender, EventArgs e)
        {
            queryTextBox.SelectedText = "";
        }
        private void messageToTextContextMenuItem_Click(object sender, EventArgs e)
        {
            string tmp = "@MessageToText(" + queryTextBox.SelectedText + ")";
            Clipboard.SetText(tmp);
            queryTextBox.Paste();
        }
        private void pasteContextMenuItem_Click(object sender, EventArgs e)
        {
            queryTextBox.Paste();
        }
        private void queryTextBox_TextChanged(object sender, FastColoredTextBoxNS.TextChangedEventArgs e)
        {
            if (_currentFile.Length > 0)
            {
                SetDirty(true);
            }
        }
        private void selectAllContextMenuItem_Click(object sender, EventArgs e)
        {
            queryTextBox.SelectAll();
        }
        private void undoContextMenuItem_Click(object sender, EventArgs e)
        {
            queryTextBox.Undo();
        }
        #endregion
    }
}
